﻿using EF6WithSqlDemo.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Linq;

namespace EF6WithSqlDemo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            DBInit();

            using (var db = new BloggingContext())
            {
                var query = db.Blogs.Where(b => b.Url == "url1").Include(b => b.Posts);

                db.Configuration.LazyLoadingEnabled = true;

                var q = db.Blogs;

                string a = q.ToString();

                foreach (var blog in q)
                {
                    Console.WriteLine($"BlogId:{blog.BlogId}");

                    Console.WriteLine($"PostId:{blog.Posts.Select(p => p.Content)}");
                }
            }

            //using (var db = new BloggingContext())
            //{
            //    List<Blog> lstBlog = new List<Blog> {
            //        new Blog { Url="url1" },
            //        new Blog { Url="url2" }
            //    };

            //    db.Blogs.AddRange(lstBlog);

            //    var blog =  db.Blogs.Where(b => b.Url == "url1").First();
            //    if (blog == null)
            //    {
            //        throw new NullReferenceException("blog为空");
            //    }

            //    Post post = new Post();
            //    post.Blog = blog;
            //    post.Content = "第一篇文章";

            //    db.Posts.Add(post);

            //    int c = db.SaveChanges();

            //    Console.WriteLine($"count:{c}");
            //}

            Console.ReadLine();
        }

        /// <summary>
        /// 预生成ViewMapping
        /// 参考：http://www.cnblogs.com/dudu/p/entity-framework-warm-up.html
        /// </summary>
        public static void PreGenerateViewMapping()
        {
            using (var dbcontext = new BloggingContext())
            {
                var objectContext = ((IObjectContextAdapter)dbcontext).ObjectContext;
                var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
                mappingCollection.GenerateViews(new List<EdmSchemaError>());
            }
        }

        public static void ManageTPT()
        {
            using (var context1 = new InheritanceContext())
            {
                var worker = new Worker
                {
                    AnnualSalary = 20000,
                    Age = 25,
                    FirstName = "Joe",
                    LastName = "Plumber"
                };
                var retired = new Retired
                {
                    MonthlyPension = 1500,
                    Age = 22,
                    FirstName = "Mike",
                    LastName = "Smith"
                };

                //    Make sure the tables are empty…
                foreach (var entity in context1.Persons)
                {
                    context1.Persons.Remove(entity);
                }
                context1.Persons.Add(worker);
                context1.Persons.Add(retired);

                context1.SaveChanges();
            }

            using (var context2 = new InheritanceContext())
            {
                Console.WriteLine("Persons count:  " + context2.Persons.OfType<PersonBase>().Count());
                Console.WriteLine("Worker:  " + context2.Persons.OfType<Worker>().Count());
                Console.WriteLine("Retired:  " + context2.Persons.OfType<Retired>().Count());
            }
        }

        /// <summary>
        /// 数据库初始化
        /// </summary>
        private static void DBInit()
        {
            //Database.SetInitializer( new CreateDatabaseIfNotExists<BloggingContext>()); //默认行为

            //Database.SetInitializer(new DropCreateDatabaseAlways<BloggingContext>());

            Database.SetInitializer(new MigrateDatabaseToLatestVersion<BloggingContext, Migrations.Configuration>());
        }
    }
}